#include "oled_fun.h"
#include <Arduino.h>
#include <WiFiManager.h>
#include <nvs_flash.h>
#include <Adafruit_SSD1306.h>
#include "common_util.h"

// 屏幕分辨率
uint8_t OLED_WIDTH = 128;
uint8_t OLED_HEIGHT = 64;
// 复位引脚
uint8_t OLED_RESET = -1;  // 不使用
Adafruit_SSD1306 oled;

// oled屏幕i2c地址
uint8_t oled_addr = 0;
char* oled_addr_key = "o_a_key";
// oled像素大小，默认为 1
uint8_t oled_ts = 1;
char* oled_ts_key = "o_ts_key";

bool olde_fail = true;

void OLEDInit() {
  Serial.println("oled only SSD1306 128*64");
  nvs_get_u8(my_nvs_handle_t, oled_addr_key, &oled_addr);
  Serial.print("oled_addr:");
  Serial.print(oled_addr);
  Serial.println("   oled_a60   oled_a61");
  if (oled_addr == 0) {
    return;
  }
  oled = Adafruit_SSD1306(OLED_WIDTH, OLED_HEIGHT, &Wire, OLED_RESET);
  if (!oled.begin(SSD1306_SWITCHCAPVCC, oled_addr)) {
    Serial.println("OLED init fail!");
    return;
  }
  olde_fail = false;
  nvs_get_u8(my_nvs_handle_t, oled_ts_key, &oled_ts);
  Serial.print("oled_text_size:");
  Serial.print(oled_ts);
  Serial.println("   oled_t1   oled_t2");
  oled.clearDisplay();               // 清空缓存
  oled.setTextSize(oled_ts);         // 设置像素大小，不设置默认为 1
  oled.setTextColor(SSD1306_WHITE);  // 设置亮色显示
  oled.cp437(true);                  // Code Page 437（IBM PC ASCII），有 256 个字符
}

void oledPrint(int16_t x, int16_t y, String s) {
  if (olde_fail) {
    return;
  }
  oled.setCursor(x, y);  // 设置开始字符的左上角坐标
  for (uint8_t i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    if (c >= 32 && c <= 126) {
      oled.write(c);
    }
  }
  oled.display();  // 将写入缓存的内容显示到 OLED
}

uint8_t requestOLED(String request, int len) {
  if (oled_addr == 0 || (!(request.startsWith("OLED_") || request.startsWith("oled_")))) {
    return 0;
  }
  if (len == 5) {
    oled.clearDisplay();
    oled.display();
    return 1;
  }
  if (len < 6) {
    return 1;
  }
  char c5 = request.charAt(5);
  if ((c5 == 'A' || c5 == 'a') && len >= 8) {
    uint8_t addr = (request.charAt(6) - '0') * 10 + (request.charAt(7) - '0');
    Serial.print("new oled addr:");
    Serial.println(addr);
    saveVlaueU8(oled_addr_key, addr);
    return 1;
  }
  if ((c5 == 'T' || c5 == 't') && len >= 7) {
    uint8_t ts = (request.charAt(6) - '0');
    Serial.print("new oled text size:");
    Serial.println(ts);
    saveVlaueU8(oled_ts_key, ts);
    return 1;
  }
  if ((c5 == 'W' || c5 == 'w') && len >= 11) {
    oled.drawPixel((request.charAt(6) - '0') * 100 + (request.charAt(7) - '0') * 10 + (request.charAt(8) - '0'), (request.charAt(9) - '0') * 10 + (request.charAt(10) - '0'), SSD1306_WHITE);  // 指定横纵坐标和颜色
    oled.display();
    return 1;
  }
  if ((c5 == 'B' || c5 == 'b') && len >= 11) {
    oled.drawPixel((request.charAt(6) - '0') * 100 + (request.charAt(7) - '0') * 10 + (request.charAt(8) - '0'), (request.charAt(9) - '0') * 10 + (request.charAt(10) - '0'), SSD1306_BLACK);  // 指定横纵坐标和颜色
    oled.display();
    return 1;
  }
  if ((c5 == 'M' || c5 == 'm') && len >= 17) {
    int16_t x = (request.charAt(6) - '0') * 100 + (request.charAt(7) - '0') * 10 + (request.charAt(8) - '0');
    int16_t y = (request.charAt(9) - '0') * 10 + (request.charAt(10) - '0');
    // uint8_t bitmap[];
    int16_t w = (request.charAt(11) - '0') * 100 + (request.charAt(12) - '0') * 10 + (request.charAt(13) - '0');
    int16_t h = (request.charAt(14) - '0') * 10 + (request.charAt(15) - '0');
    uint16_t color;
    if (request.charAt(16) == 'W' || request.charAt(16) == 'w') {
      color = SSD1306_WHITE;
    } else {
      color = SSD1306_BLACK;
    }
    request = request.substring(17);
    if (request.length() % 2 != 0) {
      return 1;
    }
    int16_t dl = request.length() / 2;
    uint8_t bitmap[dl];
    for (int16_t i = 0; i < dl; i++) {
      bitmap[i] = s2h(request.charAt(i * 2)) * 16 + s2h(request.charAt(i * 2 + 1));
      // Serial.println(bitmap[i]);
    }
    oled.drawBitmap(x, y, bitmap, w, h, color);
    oled.display();
    return 1;
  }
  if (c5 == 'C' || c5 == 'c') {
    oled.clearDisplay();
  }
  if (len >= 12) {
    oled.setCursor((request.charAt(8) - '0') * 100 + (request.charAt(9) - '0') * 10 + (request.charAt(10) - '0'), (request.charAt(6) - '0') * 10 + (request.charAt(7) - '0'));  // 设置开始字符的左上角坐标
    for (uint8_t i = 11; i < len; i++) {
      char c = request.charAt(i);
      if (c >= 32 && c <= 126) {
        oled.write(c);
      }
    }
    oled.display();  // 将写入缓存的内容显示到 OLED
    return 1;
  }
  return 1;
}
